<?php

namespace app\controllers;

use Yii;
use app\models\ClubInfo;
use app\models\ClubInfoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ClubInfoController implements the CRUD actions for ClubInfo model.
 */
class ClubInfoController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ClubInfo models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ClubInfoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ClubInfo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ClubInfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ClubInfo();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->club_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionMyuser($id){

        return $this->render('my_agent_data', [
            'model' => ClubInfo::findOne($id),
        ]);
    }

    public function actionMyuserAjax($id,$start_day,$end_day){
            $sql = "select up.user_id, ui.name,ci.name as agent_name , up.user_agent_id,table_id,poker_name,sum(score) as score, sum(in_score) as in_score,sum(out_score) as out_score ,sum(end_score) as end_score   from   user_poker up , user_info ui, user_info uii,club_info ci where up.user_id = ui.user_id  and up.club_id = ci.club_id and   up.club_id = ".$id." and up.ts >= '".$start_day."' and up.ts <= '".$end_day."' group by up.user_id order by score desc; ";
            $rawData = Yii::$app->db->createCommand($sql)->queryAll();
            
            echo json_encode($rawData);
    }

    public function actionMyuserdayAjax($id,$start_day,$end_day){


          $sql = "select up.user_id, date_format( up.ts,'%Y-%m-%d') as ts ,ui.name,ci.name as agent_name , up.user_agent_id,table_id,poker_name,sum(score) as score, sum(in_score) as in_score,sum(out_score) as out_score ,sum(end_score) as end_score   from   user_poker up , user_info ui, user_info uii,club_info ci where up.user_id = ui.user_id  and up.club_id = ci.club_id and  up.club_id = ".$id." and up.ts >= '".$start_day."' and up.ts <= '".$end_day."' group by ts order by ts desc; ";
            $rawData = Yii::$app->db->createCommand($sql)->queryAll();
            
            echo json_encode($rawData);

    }

    /**
     * Updates an existing ClubInfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->club_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ClubInfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ClubInfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ClubInfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ClubInfo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
